﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PublicTransport.Services.BO;
using PublicTransport.Views.DTO;
using PublicTransport.DAL.DTO;
using PublicTransport.GpsNavigation;
using PublicTransport.Services;


namespace PublicTransport.Views
{
    public partial class ResultView : Form
    {
        private List<List<PublicTransport.Services.Step2.Transit>> transits;
        private int position = 0;
        private MapView mapViewForm;

        public ResultView(String startStation, String destinationStation, List<List<PublicTransport.Services.Step2.Transit>> transits, GpsPoint startStationGpsPoint)
        {
            InitializeComponent();
            
            mapViewForm = new MapView(startStationGpsPoint);
            cb_mapType.SelectedIndex = 0;

            ILocationService locationService = LocationService.Instance;
            locationService.LocationChanged += new Microsoft.WindowsMobile.Samples.Location.LocationChangedEventHandler(locationService_LocationChanged);
            locationService.Start();

            startLbl.Text = startStation;
            destinationLbl.Text = destinationStation;
            previousResultMenuItem.Enabled = false;

            this.transits = transits;

            DisplayResult(transits);
            dataGrid1.TableStyles.Clear();

            Graphics g = this.CreateGraphics();
            int timeWidth = (int)g.MeasureString("00:00 ", dataGrid1.Font).Width;
            int lineWidth = (int)g.MeasureString("Spojdd", dataGrid1.Font).Width;
            //int travelTimeWidth = (int)g.MeasureString("Dlzkaaa", dataGrid1.Font).Width;
            int singleSpaceWidth = (int)g.MeasureString("aa", dataGrid1.Font).Width;
            int destinationWidth = this.dataGrid1.Width - timeWidth - lineWidth - singleSpaceWidth;
            /*
            int total = timeWidth + lineWidth + timeWidth + destinationWidth;
            if (total> dataGrid1.Width)
            {
                destinationWidth= destinationWidth - (total-dataGrid1.Width);
            }
             */
            if (destinationWidth < timeWidth)
            {
                destinationWidth = (int)g.MeasureString("Destination", dataGrid1.Font).Width;
            }



            DataGridTableStyle ts = new DataGridTableStyle();
            DataGridColumnStyle cs;

            ts.MappingName = transits[0].GetType().Name;

            cs = new DataGridTextBoxColumn();
            cs.HeaderText = "Čas";
            cs.Width = timeWidth;
            cs.MappingName = "Time";
            ts.GridColumnStyles.Add(cs);

            cs = new DataGridTextBoxColumn();
            cs.HeaderText = "Spoj";
            cs.MappingName = "Line";
            cs.Width = lineWidth;
            ts.GridColumnStyles.Add(cs);

            cs = new DataGridTextBoxColumn();
            cs.HeaderText = "Vystúpiť";
            cs.MappingName = "DestinationStation";
            cs.Width = destinationWidth;
            ts.GridColumnStyles.Add(cs);
            /*
            cs = new DataGridTextBoxColumn();
            cs.HeaderText = "Dĺžka";
            cs.MappingName = "TravelTime";
            cs.Width = travelTimeWidth;
            ts.GridColumnStyles.Add(cs);
            */
            dataGrid1.TableStyles.Add(ts);

            /*
            dataGrid1.DataSource = transit;

            
            DataGridTextBoxColumn timeColumn = new DataGridTextBoxColumn();
           
            DataGridTableStyle tableStyle = new DataGridTableStyle();
            tableStyle.MappingName = transit.GetType().ToString();

            tableStyle.GridColumnStyles.Add(timeColumn);
            dataGrid1.TableStyles.Clear();
            dataGrid1.TableStyles.Add(tableStyle);
             */
        }

        private void DisplayResult(List<List<PublicTransport.Services.Step2.Transit>> transit)
        {
            List<PublicTransport.Services.Step2.Transit> currentTransit = transits[position];
            dataGrid1.DataSource = TransitWrapperDTO.Convert(currentTransit);
            startTime.Text = currentTransit[0].Time.ToString();
            destinationTime.Text = currentTransit[currentTransit.Count - 1].ArrivalTime.ToString();
        }

        private void transitBOBindingSource_CurrentChanged(object sender, EventArgs e)
        {

        }

        private void ResultView_Load(object sender, EventArgs e)
        {

        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            position++;
            DisplayResult(transits);
            CheckPaging();
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            position--;
            DisplayResult(transits);
            CheckPaging();
        }

        private void CheckPaging()
        {
            if (position >= transits.Count - 1)
            {
                nextResultMenuItem.Enabled = false;
            }
            else
            {
                nextResultMenuItem.Enabled = true;
            }

            if (position <= 0)
            {
                previousResultMenuItem.Enabled = false;
            }
            else
            {
                previousResultMenuItem.Enabled = true;
            }
        }

        /// <summary>
        /// Event vyvolaný v prípade zmeny GPS koordinátov zariadenia
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        void locationService_LocationChanged(object sender, Microsoft.WindowsMobile.Samples.Location.LocationChangedEventArgs args)
        {
            if (!FakeGps.Instance.enabled()) mapViewForm.setActualCoordinates(new GpsPoint(args.Position.Latitude, args.Position.Longitude));
            else mapViewForm.setActualCoordinates(FakeGps.Instance.getCoordinates());
        }

        private void btn_zobrazMapu_Click(object sender, EventArgs e)
        {
            mapViewForm.ShowDialog();
        }

        private void cb_mapType_SelectedIndexChanged(object sender, EventArgs e)
        {
            mapViewForm.setMapSource(cb_mapType.SelectedIndex);
        }
    }
}